﻿using System;
using System.Collections.Generic;
using System.Text;
using METODESA.Server.Data.Exceptions;
using METODESA.Server.Data.METODESA.Entity;
using NHibernate;

namespace METODESA.Server.Data.METODESA.Nhibernate
{
    public class ClienteDao:BaseDao
    {

        //Mostrar todos los Clientes.
        public static IList<Cliente> getAllClient()
        {
            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                return myISession.CreateCriteria(typeof(Cliente)).List<Cliente>();
            }
            catch (Exception e)
            {
                throw new ClienteNotExistsException("There's no client");
                return null;
            }
        }

        //  Mostrar Clientes por Cédula
        public static IList<Cliente> GetClientByCedula(string cedula)
        {
            IList<Cliente> clt = null;
            Boolean flag = true;

            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                ICriteria myCriteria = myISession.CreateCriteria(typeof(Cliente));
                myCriteria.Add(NHibernate.Expression.Expression.Eq("CedulaRif", cedula));
                clt = myCriteria.List<Cliente>();
                flag = false;
            }
            catch (HibernateException e)
            {
                throw new ClienteNotExistsException("There's no client with the provided ci");
            }

            if (flag == true)
            {
                return null;
            }

            return clt;
        }

        // Mostrar Clientes por Nombre 
        public static IList<Cliente> GetClientByName(string Nombre)
        {
            Boolean flag = true;

            IList<Cliente> clt = null;

            try
            {

                myISession = NHibernateHelper.GetCurrentSession();
                ICriteria myCriteria = myISession.CreateCriteria(typeof(Cliente));
                myCriteria.Add(NHibernate.Expression.Expression.Like("Nombre",'%'+Nombre+'%'));
                clt = myCriteria.List<Cliente>();
                flag = false;

            }
            catch (HibernateException e)
            {
                throw new ClienteNotExistsException("There's no client with the provided name");
            }

            if (flag == true)
            {
                return null;
            }

            return clt;
        }

        // Mostrar Clientes por Apellido
        public static IList<Cliente> GetClientByLastName(string Apellido)
        {
            Boolean flag = true;

            IList<Cliente> clt = null;

            try
            {

                myISession = NHibernateHelper.GetCurrentSession();
                ICriteria myCriteria = myISession.CreateCriteria(typeof(Cliente));
                myCriteria.Add(NHibernate.Expression.Expression.Like("Apellido", '%' + Apellido + '%'));
                clt = myCriteria.List<Cliente>();
                flag = false;

            }
            catch (HibernateException e)
            {
                throw new ClienteNotExistsException("There's no client with the provided Lastname");
            }

            if (flag == true)
            {
                return null;
            }

            return clt;
        }
        public static Cliente getClienteById(int id)
        {
            Cliente cliente = null;
            Boolean flag = true;

            try
            {
                myISession = NHibernateHelper.GetCurrentSession();
                cliente = (Cliente)myISession.Load(typeof(Cliente), id);
                flag = false;
            }
            catch (HibernateException e)
            {
                throw new ClienteNotExistsException("There's no client with the provided id");

            }

            if (flag)
            {
                //throw new ClientNotExistsException("There's no client with the provided id");
                return null;
            }

            return cliente;
        }
    }
}
